package com.qk.controller;


import com.qk.common.Result;
import com.qk.utils.AliyunOSSOperator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

@Slf4j
@RestController
@RequestMapping("/upload")
public class UploadController {


    @Autowired
    private  AliyunOSSOperator aliyunOSSOperator;

    @PostMapping
    public Result upload(MultipartFile image) throws Exception {
        log.info("开始上传{}", image);

        UUID uuid = UUID.randomUUID();
        String wjm = image.getOriginalFilename();
        String extName = wjm.substring(wjm.lastIndexOf("."));
        String url = aliyunOSSOperator.upload(image.getBytes(), uuid + extName);

        return Result.success(url);
    }


    //    //上传文件测试接口
//    @PostMapping
//    public Result upload(String username, Integer age , MultipartFile file) throws Exception {
//        log.info("开始上传{},{},{}", username, age, file);
//        //拿取后缀名
//        String wjm = file.getOriginalFilename();
//        String extName = wjm.substring(wjm.lastIndexOf("."));
//        //构建不重复的名称
//        UUID uuid = UUID.randomUUID();
//
//        file.transferTo(new File("D:\\test\\"+ uuid+ extName));
//        return Result.success();
//    }

}
